﻿namespace GreenIsomer.Data.Entities.Ddl {
	using System;
	using System.Xml.Serialization;

	[Serializable]
	public class Index {

		[NonSerialized]
		private ColumnRefCollection columns;

		public Index() {
			columns = new ColumnRefCollection();
		}

		[XmlAttribute("Name")]
		public string Name { get; set; }

		[XmlAttribute("Unique")]
		public bool Unique { get; set; }

		[XmlElement("Column")]
		public ColumnRefCollection Columns {
			get { return columns; }
		}


		public virtual void WriteCreateScript(DdlWriter writer, string tableName, string schema) {

			writer.StartNewLine();

			if(Unique) writer.WriteScript("CREATE UNIQUE INDEX");
			else writer.WriteScript("CREATE INDEX");

			writer.AddSpace();
			writer.WriteName(Name);
			writer.AddSpace();

			writer.WriteScript("ON");

			writer.AddSpace();
			writer.WriteSchemaPrefix(schema);
			writer.WriteName(tableName);
			writer.AddSpace();

			Columns.WriteScript(writer);

			writer.WriteScript(";");
		}

	}

}